;+function($, window,undefined) {
	FrameWindow.styleText=function(doc){
		var css="" +
			".frameWindow-box{"+
				"height:100%;"+
				"width:100%;"+
				"overflow:auto;"+
				"background-color:#fff;"+
			"}";
		css+=".frameWindow-box a{"+
				"position: absolute;"+
				"top:0px;"+
				"right:0px;"+
				"font-size:14px;"+
				"background-color:#000;"+
				"color:#fff;"+
				"border:1px solid black;"+
				"border-radius:1em;"+
				"width:1em;"+
				"text-align:center;"+
				"line-height:1em;"+
				"cursor:pointer;"+
				"text-decoration: none;"+
			"}";
		css+=".frameWindow-box a:hover{"+
				"color:#9933ff;"+
			"}";
		css+=".frameWindow-box iframe{"+
				"width:100%;"+
				"height:100%;"+
				"display:block;"+
				"border:none"+
			"}";
		
		var style = doc.createElement("style");
		style.setAttribute("type", "text/css");
		if (style.styleSheet) {// IE
			style.styleSheet.cssText = css;
		} else {// w3c
			style.appendChild(doc.createTextNode(css));
		}
		var heads = doc.getElementsByTagName("head");
		if (heads.length) {
			heads[0].appendChild(style);
		} else {
			doc.documentElement.appendChild(style);
		}
	};
	
	var defaultSetting={
			width:"80%",
			height:"80%",
			title:null,
			parent:null,//only user for child window
			onclose:null,
			onload:null,
			document:window.document
	};
	/**
	 * 构造器
	 * @param setting
	 */
	function FrameWindow(setting) {
		this.setting=$.extend({},defaultSetting,setting);
		this.parent=null;
		this.windowEL = null;
		this.frameEL = null;
		this.closeEL = null;
		this.coverEL=null;
		
		this.showing=false;
		//call method
		this._init();
		this._createElement();
		this._addStyle();
		this._addEventListener();
	};
	FrameWindow.prototype={
			_init:function(){
				var setting=this.setting;
				var document=setting.document;
				//init the field value
				this.parent=setting.parent;
				this.windowEL = null;
				this.frameEL = null;
				this.closeEL = null;
				this.coverEL=new FastCover(document.body,0.5,FrameWindow.minZ++);
			},
			_createElement : function() {
				var setting=this.setting;
				var document=setting.document;
				
				//create bg border
				var table=this._createBgBorder();
				//add to window
				this.windowEL = $(table).appendTo(document.body);
				//create content box
				var contentBox=$('<div></div>').addClass("frameWindow-box");
				contentBox.appendTo(table.contentCell);
				this.closeEL = $("<a>×</a>").appendTo(contentBox);
				this.frameEL = $('<iframe frameBorder="0"></iframe>').appendTo(contentBox);
			},
			_addStyle:function(){
				//css the window
				this.windowEL.css({
					position:"absolute",
					left:"0",
					right:"0",
					top:"0",
					bottom:"0",
					margin:"auto",
					borderCollapse: "collapse",
					tableLayout:"fixed",
					border:"none",
					zIndex:FrameWindow.minZ+=2
				});
			},
			_createBgBorder : function() {
				var document=this.setting.document;
				var table = document.createElement("table");
				var cells = [];
				var i, j, tr, td;
				for (i = 0; i < 3; i++) {
					tr = table.insertRow(-1);
					cells[i] = [];
					for (var j = 0; j < 3; j++) {
						td = tr.insertCell(-1);
						td.style.padding="0";
						cells[i][j] = td;
					}
				}
				var setTdStyle = function(td, w, h, img, repeat, px, py) {
					if (w > 0) {
						td.style.width = w + "px";
					}
					if (h > 0) {
						td.style.height = h + "px";
					}
					td.style.backgroundColor = "transparent";
					td.style.backgroundImage = "url(" + img + ")";
					td.style.backgroundRepeat = repeat;
					td.style.backgroundPosition = px + "px " + py + "px";
				};
				// lt
				setTdStyle(cells[0][0], 24, 24, "", "no-repeat", 0, 0);
				// rt
				setTdStyle(cells[0][2], 24, 24, "", "no-repeat", 0, 0);
				// lb
				setTdStyle(cells[2][0], 24, 24, "", "no-repeat", 0, 0);
				// rb
				setTdStyle(cells[2][2], 24, 24, "", "no-repeat", 0, 0);
				// ct
				setTdStyle(cells[0][1], -1, -1, "", "repeat-x", 0, 0);
				// cb
				setTdStyle(cells[2][1], -1, -1, "", "repeat-x", 0, -24);
				// ml
				setTdStyle(cells[1][0], -1, -1, "", "repeat-y", 0, 0);
				// mr
				setTdStyle(cells[1][2], -1, -1, "", "repeat-y", -24, 0);
				
				table.contentCell=cells[1][1];
				return table;	
			},
			_addEventListener : function() {
				//close btn
				this.closeEL.click(this,function(evt){
					var toThis=evt.data;
					toThis.close(true);
				});
				this.frameEL.load(this,function(evt){
					var toThis=evt.data;
					var win=this.contentWindow;
					var setting=toThis.setting;
					//回调加载完成
					var onloadCallback=setting.onload;
					if(onloadCallback){
						onloadCallback.apply(win);
					}
					//添加窗口关闭监听
					win.close=function(){
						toThis.close();
					};
				});
				
			},
			_removeEventListener:function(){
				this.closeEL.unbind("click");
				this.frameEL.unbind("load");
			},
			_pack : function() {
				var setting=this.setting;
				
				//judge window width and height
				this.windowEL.css({
					width:setting.width,
					height:setting.height,
				});
			},
			_close:function(){
				if(this.parent){
					FrameWindow.currentIntance=this.parent;
					//remove child window
					this.destory();
				}else{
					var contentWindow=this.frameEL[0].contentWindow;
					contentWindow.document.body.innerHTML="";
					this.windowEL.hide();
					this.coverEL.hide();
					this.showing=false;
				}
			}
	};
	
	
	
	FrameWindow.prototype.load = function(callback){
		if(callback){
			this.setting.onload=callback;
		}
	};
	FrameWindow.prototype.show = function(url,setting) {
		if(this.showing)return this;
		this.frameEL.attr("src",url);
		this.coverEL.show();
		this.showing=true;
		this.windowEL.show();
		if(setting){
			this.setting=$.extend(this.setting,setting);
			this._pack();
		}
		return this;
		
	};
	FrameWindow.prototype.close = function() {
		//set callback
		if(typeof arguments[0]==="function"){
			var callback=arguments[0];
			if(typeof callback==="function"){
				this.setting.onclose=callback;
			}
			return;
		}
		var fireEvent=true;
		if(typeof arguments[0]==="boolean"){
			fireEvent=arguments[0];
		}
		if(fireEvent){
			var oncloseCallback=this.setting.onclose;
			var contentWindow=this.frameEL[0].contentWindow;
			if(oncloseCallback){
				var notClose=oncloseCallback.apply(contentWindow);
				//仅等于false时不关闭
				if(notClose!==false){
					this._close();
				}
			}
		}
		this._close();
	};
	//destroy
	FrameWindow.prototype.destroy=function(){
		this._removeEventListener();
		this.coverEL.clear();
		this.windowEL.remove();
		this.parent=null;
		this.setting=null;
		this.frameEL = null;
		this.closeEL = null;
		this.coverEL=null;
		this.showing=null;
	};
	/*****************static method and variable*********************************/
	//you can set min zIndex
	FrameWindow.minZ=999;
	//save current window
	FrameWindow.currentIntance=null;
	//core method 
	FrameWindow.show=function(url,setting){
		var instance=FrameWindow.currentIntance;
		if(!instance){//no exist ,create one
			if(FrameWindow.styleText){
				FrameWindow.styleText(setting.document);
				delete 	FrameWindow.styleText;
			}
			instance = new FrameWindow();
			FrameWindow.currentIntance=instance;
		}
		//if showing then create child window
		if(instance.showing){
			//create child window
			var child=new FrameWindow({
				parent:instance
			});
			FrameWindow.currentIntance=child;
			child.show(url,setting);
		}else{
			instance.show(url,setting);
		}
		return instance;
	};
	/***************FastCover***********************/
	var FastCover=function(target,opacity,zIndex){
		this.coverTarget=$(target||window.document.body);
		this.document=this.coverTarget[0].ownerDocument||window.document;
		this.coverEL=null;
		this.opacity=opacity||0.5;
		this.zIndex=zIndex||9;
		this.showing=false;
		this._create();
	};
	FastCover.prototype._create=function(){
		var target=this.coverTarget;
		var pos=target.offset();
		this.coverEL=$("<div></div>").css({
			position:"absolute",
			top:pos.top,
			left:pos.left,
			width:target.width(),
			height:target.height(),
			backgroundColor:"#FFF",
			opacity:this.opacity,
			filter:"alpha(opacity="+(this.opacity*100)+")",
			display:"none"
		}).appendTo(this.document.body);
	};
	FastCover.prototype.show=function(){
		if(this.showing){
			return;
		}
		this.showing=true;
		this.coverEL.css("display","block");
	};
	FastCover.prototype.hide=function(){
		this.showing=false;
		this.coverEL.css("display","none");
	};
	FastCover.prototype.clear=function(){
		this.coverEL.remove();
	};
	//expose
	$.frameWindow = function(url,setting) {
		var fw=FrameWindow.show(url,$.extend({document:window.document},setting));
		return fw;
	};
}(jQuery,window.top);
$(function(){
	$(".popupWindow").each(function() {
	    $(this).click(function(evt) {
	       $.frameWindow(this.href, {
	          width : "80%",
	          height : "80%",
	          title : this.title || this.innerHTML
	       });
	       evt.preventDefault();
	    });
	 });
});